﻿using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using WCF.NinjectDependencyInjectionDemo.Models;
using WCF.NinjectDependencyInjectionDemo.Repositories;

namespace WCF.NinjectDependencyInjectionDemo.Services
{
    [ServiceContract]
    public interface IContactService
    {
        [OperationContract]
        [WebGet(UriTemplate = "contact")]
        List<Contact> GetAllContacts();

        [OperationContract]
        [WebGet(UriTemplate = "contact?id={id}")]
        Contact GetContact(int id);

        [OperationContract]
        [WebInvoke(UriTemplate = "contact", Method = "POST")]
        Contact SaveContact(Contact contact);

        [OperationContract]
        [WebInvoke(UriTemplate = "contact", Method = "DELETE")]
        bool RemoveContact(Contact contact);
    }

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ContactService : IContactService 
    {
        public IContactRepository ContactRepository { get; set; }

        public ContactService(IContactRepository contactRepository)
        {
            ContactRepository = contactRepository;
        }

        public List<Contact> GetAllContacts()
        {
            return ContactRepository.GetAll();
        }

        public Contact GetContact(int id)
        {
            return ContactRepository.GetById(id);
        }

        public Contact SaveContact(Contact contact)
        {
            ContactRepository.Save(contact);
            return contact;
        }

        public bool RemoveContact(Contact contact)
        {
            ContactRepository.Remove(contact.Id);
            return true;
        }
    }
}
